package cn.laixueit.controller;

import cn.laixueit.pojo.Easy;
import cn.laixueit.service.EasyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/easy")
public class EasyController {
    @Autowired
    private EasyService easyService;

    @RequestMapping("/findall")
    public String findall(@RequestParam(defaultValue = "1")Integer startpage, @RequestParam(defaultValue = "2")Integer pagesize,Easy easy, Model model){
        PageInfo<Easy> pageInfo = easyService.findall(startpage, pagesize,easy);
        model.addAttribute("pageinfo",pageInfo);
        return "main";
    }

    @PostMapping("/add")
    public String add(Easy easy){
        System.out.println("easy = " + easy);
        easyService.add(easy);
        return "redirect:findall";
    }

    @GetMapping("/findById")
    public String findById(String id,Model model){
        Easy easy = easyService.findById(id);
        model.addAttribute("esay",easy);
        return "edit";
    }

    @PostMapping("/update")
    public String update(Easy easy){
        easyService.update(easy);
        return "redirect:findall";
    }

    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id")String id){
        easyService.deleteById(id);
        return "redirect:/easy/findall";
    }


}
